package com.screen.screendisplay.service.impl.wholeproduct;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.wholeproduct.WholeOverviewItem;
import com.screen.screendisplay.mapper.wholeproduct.WholeOverviewItemMapper;
import com.screen.screendisplay.service.wholeproduct.WholeOverviewItemService;
import com.screen.screendisplay.vo.wholeproduct.WholeProductOverviewVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 全产品主要指标情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-10-03
 */
@Service
public class WholeOverviewItemServiceImpl extends ServiceImpl<WholeOverviewItemMapper, WholeOverviewItem> implements WholeOverviewItemService {
    
    @Override
    public WholeProductOverviewVO getLatestOverview(Integer year) {
        // 构建查询条件
        QueryWrapper<WholeOverviewItem> queryWrapper = new QueryWrapper<>();
        
        // 如果指定了年份，则根据年份查询
        if (year != null) {
            queryWrapper.eq("year", year);
        }
        
        // 按时间倒序排列，取最新的一条数据
        queryWrapper.orderByDesc("year", "month", "day").last("LIMIT 1");
        
        WholeOverviewItem entity = this.getOne(queryWrapper);
        
        // 转换为VO对象
        WholeProductOverviewVO vo = new WholeProductOverviewVO();
        if (entity != null) {
            vo.setTotalAssets(entity.getTotalAssets());
            vo.setOperatingIncome(entity.getOperatingIncome());
            vo.setTotalProfit(entity.getTotalProfit());
            vo.setEconomicValueAdded(entity.getEconomicValueAdded());
            vo.setComprehensiveOperationIndex(entity.getComprehensiveOperationIndex());
        }
        
        return vo;
    }
}
